package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.CD;
import com.jwinf.cdLibary.service.CDService;
import com.jwinf.cdLibary.tool.WindowUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author jwinf
 * 光盘管理窗口控制类
 */
public class LookCdController implements Initializable {
    @FXML
    private Button newButton;
    @FXML
    private Button editButton;
    @FXML
    private Button deleteButton;
    @FXML
    private TextField serchtxt;
    @FXML
    private Label searchIcon;
    @FXML
    private TableView table;
    @FXML
    private Label goback;

    private CDService cdService = new CDService();
    private ObservableList<CD> list;

    // 初始化方法
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        componentInit();                 // 组件初始化
        addListener();                   // 添加监听器
    }

    // 组件初始化
    private void componentInit(){

        // 改变标签的icon样式
        Image image;
        image = new Image(getClass().getResourceAsStream("/icon/findIcon.png"), 32, 32, false, false);
        searchIcon.setGraphic(new ImageView(image));

        image = new Image(getClass().getResourceAsStream("/icon/backIcon.png"), 32, 32, false, false);
        goback.setGraphic(new ImageView(image));

        // 给表格新增列 并为列绑定相关的cd属性
        TableColumn codeCol = new TableColumn("编号");
        codeCol.setCellValueFactory(new PropertyValueFactory<>("code"));
        codeCol.setPrefWidth(100);
        TableColumn nameCol = new TableColumn("名字");
        nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        nameCol.setPrefWidth(100);
        TableColumn sortCol = new TableColumn("分类");
        sortCol.setCellValueFactory(new PropertyValueFactory<>("sort"));
        sortCol.setPrefWidth(100);
        TableColumn numCol = new TableColumn("数量");
        numCol.setCellValueFactory(new PropertyValueFactory<>("number"));
        numCol.setPrefWidth(100);
        TableColumn stateCol = new TableColumn("状态");
        stateCol.setCellValueFactory(new PropertyValueFactory<>("state"));
        stateCol.setPrefWidth(100);

        // 给表格添加列
        table.getColumns().addAll(codeCol, nameCol, sortCol, numCol, stateCol);
    }

    // 添加监听器方法
    private void addListener(){
        // 在鼠标进入控件时改变鼠标的样式
        WindowUtils.changeMouse(searchIcon);
        WindowUtils.changeMouse(goback);
        WindowUtils.changeMouse(newButton);
        WindowUtils.changeMouse(editButton);
        WindowUtils.changeMouse(deleteButton);
        // 在执行耗时操作时改变鼠标的样式
        WindowUtils.MouseWait(searchIcon);
        // 鼠标双击表格触发编辑操作
        table.addEventHandler(MouseEvent.MOUSE_CLICKED, e->{
            if(e.getButton() == MouseButton.PRIMARY)
                if(e.getClickCount() >= 2) {
                    try {
                        edit();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }

        });
    }

    // 查询方法
    @FXML
    private void search(){
        // 获取组件中的数据
        String cdName = serchtxt.getText();
        // 检验数据的合法性 并弹出弹窗提示用户
        if(cdName == null || cdName.isEmpty()){
           WindowUtils.showTip("请输入检索词", "/icon/warnIcon.png", " -fx-background-color:  linear-gradient(to bottom, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            return;
        }
        // 调用服务层的查询方法 并将其转换为表格支持的数据类型
        list = FXCollections.observableArrayList(cdService.search(cdName));

        // 给表格设置数据
        if(!list.isEmpty())
            table.setItems(list);
        else{
            WindowUtils.showTip("查无此盘", "/icon/warnIcon.png", " -fx-background-color:  linear-gradient(to bottom, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            table.setItems(null);
        }

    }

    // 返回管理员主界面方法
    @FXML
    private void back() throws IOException {
        WindowUtils.showFrame("/com/jwinf/cdLibary/view/AdminMainFrame.fxml", "/icon/loginIcon.png", "管理员主界面");
        WindowUtils.destroyStage(goback);
    }

    // 弹出添加窗体方法
    @FXML
    private void add() throws IOException {
        WindowUtils.showaddcdFrame();
        WindowUtils.destroyStage(newButton);
    }

    // 弹出编辑窗体
    @FXML
    private void edit() throws IOException {
        // 获取选中的行号
        int index = table.getSelectionModel().getSelectedIndex();
        if(index < 0){
            WindowUtils.showTip("请选择编辑对象", "/icon/warnIcon.png", " -fx-background-color:  linear-gradient(to bottom, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            return;
        }
        // 获取选中的对象
        CD cd = list.get(index);
        // 注入选中的对象 并显示编辑窗体
        WindowUtils.showeditFrame(cd);
        // 销毁当前窗体
        WindowUtils.destroyStage(editButton);
    }

    // 删除对象方法
    @FXML
    private void delete(){
        // 获取选择的行号
        int index = table.getSelectionModel().getSelectedIndex();
        if(index < 0){
            WindowUtils.showTip("请选择删除对象", "/icon/warnIcon.png", " -fx-background-color:  linear-gradient(to bottom, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            return;
        }
        // 调用服务层的删除方法
        if(ButtonType.OK == WindowUtils.inquiryAlert("确认删除该光盘？", " -fx-background-color:  linear-gradient(to bottom, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);").get());{
            CD cd = list.get(index);
            cdService.delete(cd);
        }
    }
}
